<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick="remove()">删除</button>
  <button onclick="add()">新增</button>
  <script>
    const baseUrl = "http://127.0.0.1:3000"

    const require = {
      get(url, params) {
        // 将对象转成字符串
        // {page: 1, pagesize: 3}  ->  "page=1&pagesize=3"
        const arr = Object.keys(params) // ["page", "pagesize"];
        const arr2 = arr.map(item => {
          return item + "=" + params[item]
        })  // ["page=1", "pagesize=3"]
        const str = "?" + arr2.join("&");


        // get请求的参数直接拼接在url的后面
        return fetch(baseUrl + url + str, {
          method: "GET",
          headers: {
            'Content-Type': 'application/json',
            // 一般情况下token，要放在请求头的里面
            'token': localStorage.getItem("token"),
          },
        })
          .then(response => response.json())
      },
      post(url, body) {
        return fetch(baseUrl + url, {
          method: "POST",
          headers: {
            'Content-Type': 'application/json',
            // 一般情况下token，要放在请求头的里面
            'token': localStorage.getItem("token"),
          },
          body: JSON.stringify(body)
        })
          .then(response => response.json())
      },
    }

    // 获取列表数据
    const getData = () => {
      require.get("/students/list", {page: 1, pagesize: 3, value: ""}).then(res => {
        console.log(res);
      })
    }
    getData(1);

    // 删除
    const remove = () => {
      require.post("/students/del", {id: "6705d942d358c08136d2dbc6"}).then(res => {
        console.log(res);
      })
    }

    // 新增
    const add = () => {
      require.post("/students/add", {name: "123", num: 111, age: 222, sex: 1})
      .then(res => {
        console.log(res);
      })
    }
    
  </script>
</body>
</html>